package controller

import (
	"github.com/mjiulee/lego"
	"github.com/valyala/fasthttp"
	"saas_sys_base/common"
	lbs "saas_sys_base/lib/lbsqq/util"
	distance "saas_sys_base/lib/lbsqq/ws/distance/v1"
	"saas_sys_base/sysconfig"
)

type LbsQqApiCtrl struct {
	common.AppCtrlBase
}

func init() {
	vc := new(LbsQqApiCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *LbsQqApiCtrl) CtrlInfo() string {
	return "LbsQqApiCtrl"
}

func (vc *LbsQqApiCtrl) RoutInit() {
	lego.APIGET("/api/lbs/matrix", vc.Matrix, false)
}

func (vc *LbsQqApiCtrl) Matrix(ctx *fasthttp.RequestCtx) {

	ctxExt := lego.RequestCtxExtent{ctx}

	var params distance.MatrixParam
	params.Key = sysconfig.Config.LbsQq.Key
	params.Mode = "driving"
	params.From.Lng = 117.279396
	params.From.Lat = 39.108951

	params.To = make([]distance.LocationPoint,2)
	params.To[0].Lat = 39.071510
	params.To[0].Lng = 117.190091

	params.To[1].Lng = 116.389160
	params.To[1].Lat = 40.007632

	result := make(map[string]interface{})
	code := 0
	msg := "success"

	var client *lbs.Client = new(lbs.Client)

	var response *distance.MatrixResonse
	var err error
	response, err = distance.Matrix(client, params)

	if err != nil {
		code = -1
		msg = err.Error()
	}

	result["code"] = code
	result["msg"] = msg
	result["data"] = response
	ctxExt.JSON(200, result)
}
